<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="content">
        <div id="test">
            <h4>测试DOM</h4>
            <p class="new">这是一个段落！！</p>
            <button onclick="cal()">点击！</button>
        </div>
    </div>
</body>
<script>
    function cal() {
        var testDiv = document.getElementById("test");

        console.log(`id: ${testDiv.id}`);
        console.log(`inner html: ${testDiv.innerHTML}`);
        console.log(`${testDiv.innerText}`);

        // 遍历子节点
        if (testDiv.hasChildNodes()) {
            children = testDiv.children;
            for (let i = 0; i < children.length; i++) {
                var element = children[i];
                console.log(element);
            } 
        }

        // parent
        var parentNode = testDiv.parentNode;
        var p = document.createElement("p");
        p.innerText = "添加新元素";
        parentNode.appendChild(p);
    }
</script>
</html>